package com.content_module.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.content_module.R;

import java.util.List;

public class FlowTagViewAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private int layoutId = 0;
    private int selectionPosition = 0;
    private List<String> list;

    public FlowTagViewAdapter(Context context, int layoutId) {
        this.context = context;
        this.layoutId = layoutId;
        inflater = LayoutInflater.from(context);
    }

    public void setSelectionPosition(int selectionPosition) {
        this.selectionPosition = selectionPosition;
        notifyDataSetChanged();
    }

    public void setData(List<String> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(layoutId, parent, false);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.content_item_flow_tag_view_txt);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.textView.setText(list.get(position));
        if (selectionPosition == position) {
            viewHolder.textView.setSelected(true);
        } else {
            viewHolder.textView.setSelected(false);
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView textView;
    }
}
